package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.model.Room;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.IRoomOccupancyDao;
import com.eling.elcms.travel.model.NonMemberGroup;
import com.eling.elcms.travel.model.PlanItemGroup;
import com.eling.elcms.travel.model.RoomOccupancy;
import com.eling.elcms.travel.service.IRoomOccupancyManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoomOccupancyManagerImpl extends GenericManagerImpl<RoomOccupancy, Long> implements IRoomOccupancyManager {

	IRoomOccupancyDao roomOccupancyDao;

	@Autowired
	public void setRoomOccupancyDao(IRoomOccupancyDao roomOccupancyDao) {
		this.dao = roomOccupancyDao;
		this.roomOccupancyDao = roomOccupancyDao;
	}

	@Override
	public void removeRoomOccup(PlanItemGroup pig, Date startDate, Date endDate) {
		RoomOccupancy roomOcc = new RoomOccupancy();
		roomOcc.setPlanItemGroup(pig);
		PropertyUtils.setProperty(roomOcc, "occupyDate", startDate);
		PropertyUtils.setProperty(roomOcc, "occupyDateEnd", endDate);
		List<RoomOccupancy> roomOccList = roomOccupancyDao.query(roomOcc);
		// 删除房间占用数据
		if (!roomOccList.isEmpty()) {
			roomOccupancyDao.remove(roomOccList);
		}

	}

	@Override
	public void newRoomOccup(PlanItemGroup planItemGroup, List<Room> roomList, Set<Date> dateSet) {
		Set<RoomOccupancy> roomOccupancySet = new HashSet<>();
		for (Room rm : roomList) {
			for (Date de : dateSet) {
				RoomOccupancy ro = new RoomOccupancy();
				ro.setRoom(rm);
				ro.setPlanItemGroup(planItemGroup);
				ro.setOccupyDate(de);
				ro.setVersion(0);
				roomOccupancySet.add(ro);
			}
		}

		// 保存房间占用数据
		if (!roomOccupancySet.isEmpty()) {
			roomOccupancyDao.save(new ArrayList<>(roomOccupancySet));
		}

	}

	@Override
	public void removeRoomOccup(PlanItemGroup pig) {
		RoomOccupancy roomOcc = new RoomOccupancy();
		roomOcc.setPlanItemGroup(pig);
		List<RoomOccupancy> roomOccList = roomOccupancyDao.query(roomOcc);
		// 删除房间占用数据
		if (!roomOccList.isEmpty()) {
			roomOccupancyDao.remove(roomOccList);
		}
	}

	@Override
	public void removeRoomOccup(PlanItemGroup pig, List<Long> pkRoomList) {
		RoomOccupancy roomOcc = new RoomOccupancy();
		roomOcc.setPlanItemGroup(pig);
		PropertyUtils.setProperty(roomOcc, "room.pkRoomIn", pkRoomList);
		List<RoomOccupancy> roomOccList = roomOccupancyDao.query(roomOcc);
		// 删除房间占用数据
		if (!roomOccList.isEmpty()) {
			roomOccupancyDao.remove(roomOccList);
		}
	}

	@Override
	public void removeRoomOccup(NonMemberGroup nmg) {
		RoomOccupancy roomOcc = new RoomOccupancy();
		roomOcc.setNonMemberGroup(nmg);
		List<RoomOccupancy> roomOccList = roomOccupancyDao.query(roomOcc);
		// 删除房间占用数据
		if (!roomOccList.isEmpty()) {
			roomOccupancyDao.remove(roomOccList);
		}
	}

}
